package cloud.wgcloud.v7;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;

public class XmlToName extends Application {

    /**
     * 文件保存路径
     */
    File fileesss;
    File newImg;

    /**
     * 加载xml文件
     */
    public static Document load(){
        //获取桌面路径
        FileSystemView fileSystemView = FileSystemView.getFileSystemView();
        File file1Path = fileSystemView.getHomeDirectory();
        String userPath = System.getProperty("user.dir");

        System.out.println("userPath:"+userPath);
        System.out.println(file1Path); // /Users/guichun

        Document document=null;
        String url=userPath + "/config.xml";

        try {
            SAXBuilder reader = new SAXBuilder();
            document=reader.build(new File(url));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return document;
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        // 获取桌面路径
        FileSystemView fileSystemView = FileSystemView.getFileSystemView();
        File file1Path = fileSystemView.getHomeDirectory();

        BorderPane borderPane = new BorderPane();
        Accordion accordion = new Accordion();//手风琴

        VBox vbox = new VBox(); //手风琴
        vbox.setPadding(new Insets(10,10,10,10));

        VBox vbox1 = new VBox(5); //正方形
        vbox1.setPadding(new Insets(10,10,10,10));

        VBox vbox2 = new VBox(2); //线条
        vbox2.setPadding(new Insets(10,10,0,10));

        //事件
        MenuBar menuBar = new MenuBar();
        menuBar.prefWidthProperty().bind(primaryStage.widthProperty());

        Menu menu = new Menu("XML管理");
        MenuItem menuItem = new MenuItem("重新加载XML文件");
        menu.getItems().add(menuItem);
        menuBar.getMenus().add(menu);
        menuItem.setOnAction((event) ->{
            System.out.println("重新加载XML文件");
            accordion.getPanes().clear();

            Document document=null;
            document=load();
            Element documentRootElement = document.getRootElement();
            List<Element> elementList = documentRootElement.getChildren();

            for (Element e:elementList) {
                List<Element> elementList1 = e.getChildren();
                TitledPane titledPane = new TitledPane();
                titledPane.setText(e.getAttribute("tit").getValue());

                VBox vbox3 = new VBox(5); //手风琴内容
                vbox3.setPadding(new Insets(10,0,10,0));

                for (Element element : elementList1) {
                    List<Element> elementList2 = element.getChildren();
                    Button button2 = new Button();
                    button2.setPadding(new Insets(10,30,10,30));
                    button2.setText(element.getAttribute("tit_son").getValue());
                    vbox3.getChildren().add(button2);
                    titledPane.setContent(vbox3);

                    //手风琴-事件
                    button2.setOnAction((ActionEvent event1) ->{
                        vbox1.getChildren().clear();
                        vbox2.getChildren().clear();

                        for (Element element1 : elementList2){
                            String names = element1.getChildText("names");
                            String urls = element1.getChildText("urls");
                            String explain = element1.getChildText("explain");

                            Rectangle rectangle = new Rectangle(20,20,300, 200);

                            String s1 = "file:"+file1Path+urls +"/"+names+".png";
                            System.out.println("s1 = " + s1);
                            Image image = new Image(s1);
                            ImageView imageView = new ImageView();
                            imageView.setImage(image);
                            imageView.setFitWidth(300);
                            imageView.setFitHeight(200);
                            imageView.setX(20);
                            imageView.setY(20);

                            rectangle.setFill(Color.rgb(211,211,211,0.6));
                            rectangle.setArcWidth(15);
                            rectangle.setArcHeight(15);

                            HBox hBox2=new HBox();
                            Pane pane = new Pane();//正方形
                            pane.getChildren().addAll(rectangle,imageView);

                            if (explain != null){
                                System.out.println("explain:"+ explain);
                                Text text = new Text();
                                text.setText("备注："+explain);
                                vbox1.getChildren().add(text);
                            }if (names !=null){
                                System.out.println("names:"+ names);
                                Text text = new Text();
                                text.setText("名称："+names);
                                vbox1.getChildren().add(text);

                            }if (urls != null){
                                System.out.println("urls:"+ file1Path+urls);
                                Text text = new Text();
                                text.setText("路径："+file1Path+urls);
                                vbox1.getChildren().add(text);
                            }

                            //设置拖曳
                            rectangle.setOnDragOver(event2 -> {
                                if (event2.getGestureSource() != rectangle
                                        && event2.getDragboard().hasFiles()) {
                                    event2.acceptTransferModes(TransferMode.COPY_OR_MOVE);
                                }
                                event2.consume();
                            });
                            rectangle.setOnDragDropped(event22 -> {
                                Dragboard db = event22.getDragboard();
                                boolean success = false;
                                List<File> list = db.getFiles();

                                if (db.hasFiles()) {
                                    list.stream().forEach((file) -> {
                                        fileesss = new File(file1Path+urls);
                                        if (!fileesss.exists()) {
                                            fileesss.mkdirs();
                                        }
                                        File oldImg = new File(file.getPath());
                                        System.out.println("原图片：" + oldImg);


                                        newImg = new File(fileesss + "/" + names + ".png");

                                        System.out.println("修改后：" + newImg);
                                        try {
                                            Files.copy(oldImg.toPath(),newImg.toPath());
                                        } catch (IOException e1) {
                                            e1.printStackTrace();
                                        }
                                        button2.fire();// 调用menuItem的点击方法
                                    });
                                    success = true;
                                }
                                event22.setDropCompleted(success);

                                event22.consume();

                            });

                            vbox1.getChildren().addAll(pane);

                            Separator separator = new Separator();
                            separator.setPadding(new Insets(20,0,10,0));
                            vbox1.getChildren().add(separator);

                            hBox2.getChildren().addAll(vbox1, vbox2);
                            borderPane.setCenter(hBox2);
                        }
                    });
                }
                accordion.getPanes().add(titledPane);
            }
        });
        menuItem.fire();// 调用menuItem的点击方法

        borderPane.setTop(menuBar);
        vbox.getChildren().add(accordion);
        borderPane.setLeft(vbox);

        ScrollPane scrollPane = new ScrollPane();
        scrollPane.setPrefWidth(500);
        scrollPane.setPrefHeight(1000);
        scrollPane.setContent(borderPane);

        Scene scene = new Scene(scrollPane);
        primaryStage.setScene(scene);
        primaryStage.setTitle("ImageModificationName By GuiChun");
        primaryStage.setWidth(610);
        primaryStage.setHeight(1100);
        primaryStage.show();
    }
}
